package com.library.dao.db;

import com.library.dao.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import com.library.entity.*;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Repository
public class JdbcUserRepository implements UserRepository {
    private final JdbcTemplate jdbcTemplate;
    private final JdbcOperations jdbc;

    /**
     *
     * @param jdbc
     * @param jdbcTemplate
     */
    @Autowired
    public JdbcUserRepository(JdbcOperations jdbc, JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        this.jdbc = jdbc;
    }

    private static class UserRowMapper implements RowMapper<User> {
        /**
         *
         * @param rs
         * @param rowNum
         * @return
         * @throws SQLException
         */
        @Override
        public User mapRow(ResultSet rs,int rowNum)throws SQLException{
            return new User(
                    rs.getLong("id"),
                    rs.getString("userName"),
                    rs.getString("password"),
                    rs.getString("fullName"),
                    rs.getString("email"),
                    rs.getString("phoneNo"),
                    rs.getBoolean("locked"),
                    rs.getTime("createTime")
            );
        }
    }
    /**
     *
     * @return 包含所有user的List<user>
     */
    @Override
    public List<User> findAll(){
        List<User> users= jdbc.query("select * from users where locked=false order by id", new UserRowMapper());
        return users.isEmpty()?null:users;
    }

    /**
     *
     * @param offset 查询起点
     * @param limit  查询条数
     * @return user的List
     */
    @Override
    public List<User> findByPage(int offset,int limit){
        return jdbc.query(
                "select * from users where locked=false limit ? offset ?",
                new UserRowMapper(),limit,offset
        );
    }

    /**
     *
     * @param userName 传入userName
     * @return  user对象
     */
    @Override
    public User findByName(String userName){
        List<User> users=jdbc.query("select * from users where userName=? and locked=false", new UserRowMapper(), userName);
        return users.isEmpty()?null:users.get(0);
    }

    /**
     *
     * @return user的数据条数
     */
    @Override
    public int countUsers(){
        return jdbc.queryForObject("select count(*) from users where locked=false",Integer.class);
    }

    /**
     *
     * @param user 传入user对象，更改数据库该条数据
     */
    @Override
    public void updateInfo(User user){
        jdbc.update("update users set password=?,fullName=?,email=?,phoneNo=?",
                user.getPassword(),user.getFullName(),user.getEmail(),user.getPhoneNo());
    }

    /**
     *
     * @param userName 传入userName，将该对象locked置为true
     */
    @Override
    public void deleteByName(String userName){
        jdbc.update("update users set locked=true where userName=?",userName);
    }

    /**
     *
     * @param user 传入user对象
     */
    @Override
    public void addUser(User user){
        jdbc.update("insert into users (userName,password,fullName,email,phoneNo,createTime) values (?,?,?,?,?,now())",user.getUserName(),user.getPassword(),user.getFullName(),user.getEmail(),user.getPhoneNo());
    }

    /**
     *
     * @param password 传入新密码
     * @param id    根据id查找user
     */
    @Override
    public void updateUser(String password,long id){
        jdbc.update("update users set password=? where id=?",password,id);
    }

}
